/**
 * 
 */
package client.gui.panels;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import controllers.ControllerIF;
import controllers.VerificationController;
import entities.Message;
import entities.MsgType;
import entities.ObjectType;

/**
 * @author Vodi
 *
 */
public class VerificationPanel extends JPanel implements PanelIF, ActionListener {

	
	private static final long serialVersionUID = 1L;

	/**
	 * the execution panel controller
	 */
	private VerificationController verificationController;
	
	private Message message;
	
	
	private JButton btnConfirm;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextArea rejectionTextArea;
	private JRadioButton rejectRadioButton;
	private JRadioButton confirmRadioButton;
	
	
	public VerificationPanel() {	
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(Box.createVerticalStrut(20));
		
		JLabel promptLabel = new JLabel("Insert your verification results of the system:");
		promptLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(promptLabel);
		
		add(Box.createVerticalStrut(20));
		
		JPanel confirmPanel = new JPanel();
		confirmPanel.setBorder(new TitledBorder(null, "Confirm execution", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(confirmPanel);
		confirmPanel.setLayout(new BoxLayout(confirmPanel, BoxLayout.Y_AXIS));
		
		confirmRadioButton = new JRadioButton("Confirm finished execution");
		confirmRadioButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		buttonGroup.add(confirmRadioButton);
		confirmPanel.add(confirmRadioButton);
		confirmRadioButton.setBorder(new TitledBorder(null, "Confirm execution", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel rejectPanel = new JPanel();
		rejectPanel.setBorder(new TitledBorder(null, "Reject execution", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(rejectPanel);
		rejectPanel.setLayout(new BoxLayout(rejectPanel, BoxLayout.Y_AXIS));
		
		rejectRadioButton = new JRadioButton("Reject finished execution");
		buttonGroup.add(rejectRadioButton);
		rejectPanel.add(rejectRadioButton);
		
		rejectPanel.add(Box.createVerticalStrut(20));
		
		JScrollPane rejectionScroll = new JScrollPane();
		rejectPanel.add(rejectionScroll);
		
		rejectionTextArea = new JTextArea();
		rejectionTextArea.setToolTipText("Enter rejection report here");
		rejectionTextArea.setLineWrap(true);
		rejectionTextArea.setEditable(false);
		rejectionTextArea.setRows(6);
		rejectionScroll.setViewportView(rejectionTextArea);
		
		add(Box.createVerticalStrut(20));
		
		JPanel buttonPanel = new JPanel();
		add(buttonPanel);
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		
		buttonPanel.add(Box.createHorizontalGlue());
		
		btnConfirm = new JButton("Confirm");
		btnConfirm.addActionListener(this);
		buttonPanel.add(btnConfirm);
		
		buttonPanel.add(Box.createHorizontalStrut(20));
		
		add(Box.createVerticalStrut(20));
	}


	/* (non-Javadoc)
	 * @see client.gui.panels.PanelIF#setController(controllers.ControllerIF)
	 */
	@Override
	public void setController(ControllerIF cif) {
		this.verificationController = (VerificationController) cif;
	}

	/**
	 * Gets the message object holding the evaluator's report
	 * @return Message
	 */
	public Message getMessage() {
		return message;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(confirmRadioButton)) {
			rejectionTextArea.setEditable(false);
		}
		else if(e.getSource().equals(rejectRadioButton)) {
			rejectionTextArea.setEditable(true);
		}
		else if(e.getSource().equals(btnConfirm)) {
			if(confirmRadioButton.isSelected()) {
				message = new Message(MsgType.SET_VERIFICATION_REPORT_REQUEST, ObjectType.STRING, "APPROVED"); 
			}
			else if(rejectRadioButton.isSelected()) {
				ArrayList<String> params = new ArrayList<>();
				params.add("REJECTED");
				params.add(rejectionTextArea.getText());
				message = new Message(MsgType.SET_VERIFICATION_REPORT_REQUEST, ObjectType.MIX_ARRAY, params); 
			}
			verificationController.action();
		}
	}


	public void setPanelEditable(boolean bool) {
		rejectionTextArea.setEditable(bool);
		rejectRadioButton.setEnabled(bool);
		btnConfirm.setEnabled(bool);
		confirmRadioButton.setEnabled(bool);
	}

}
